package com.dhl.cloud.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dhl.cloud.common.Constants;
import com.dhl.cloud.common.http.HttpResult;
import com.dhl.cloud.common.http.HttpUser;
import com.dhl.cloud.common.http.Page;
import com.dhl.cloud.common.vdo.CourseBean;
import com.dhl.cloud.common.vo.ComFrontBean;
import com.dhl.cloud.common.vo.CourseFrontBean;
import com.dhl.cloud.core.entity.Dicinfo;
import com.dhl.cloud.core.service.DicinfoService;
import com.dhl.cloud.core.service.UserRoleService;
import com.dhl.cloud.course.entity.Course;
import com.dhl.cloud.course.service.CourseService;
import com.dhl.cloud.course.service.LessionService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping(value = "/user")
public class CourseController extends BaseController{

	@Autowired
	private CourseService courseService;
	@Autowired
	private DicinfoService dicinfoService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private LessionService lessionService;
    /**
     * 获取当天正要上课的lession，以课程-lession数组的形式包装给首页
     */
    @GetMapping("getLiveLession")
    public HttpResult getLiveLession(HttpServletRequest request)
    {
    	List<CourseBean> list = lessionService.selectNowLession();
        return HttpResult.success("",list);
    }

    /**
     * 获取是否教师的标记
     */
    @GetMapping("isTeacher")
    public HttpResult isTeacher(HttpServletRequest request)
    {
    	//目前系统暂无菜单，权限的分配，这儿只能先写死，判断是否具备教师的角色
        HttpUser user = getUser(request);
        String username = user.getUsername();
        boolean flag = userRoleService.isTeacher(username);
        return HttpResult.success("",flag);
    }
    /**
     * 获取是否管理员的标记
     */
    @GetMapping("isAdmin")
    public HttpResult isAdmin(HttpServletRequest request)
    {
    	//目前系统暂无菜单，权限的分配，这儿只能先写死，判断是否具备教师的角色
        HttpUser user = getUser(request);
        String username = user.getUsername();
        boolean flag = userRoleService.isAdmin(username);
        return HttpResult.success("",flag);
    }

    /**
     * 获取是否教师的标记
     */
    @GetMapping("isTeacherCourse")
    public HttpResult isTeacherCourse(HttpServletRequest request,Integer courseid)
    {
    	//目前系统暂无菜单，权限的分配，这儿只能先写死，判断是否教师自己的课程
        HttpUser user = getUser(request);
        String username = user.getUsername();
        boolean flag = courseService.selectByPrimaryKey(courseid).getUsername().equals(username);
        return HttpResult.success("",flag);
    }

    /**
     * 获取所有老师的课程列表
     */
	@PostMapping("getAllCourseList")
    public HttpResult getAllCourseList(HttpServletRequest request,@RequestBody CourseFrontBean courseFrontBean)
    {
        Integer pageNum = courseFrontBean.getPagenum();
		Integer pageSize = courseFrontBean.getPagesize();
		PageInfo<CourseBean> list = courseService.getAllCourseList(courseFrontBean.getMarjor(), pageNum, pageSize);

		Page page = new Page();
		page.setTotal((int)list.getTotal());
		page.setCurrentpage(pageNum);
		page.setPagesize(pageSize);
		return HttpResult.success("",page,list.getList());

    }


    /**
     * 获取我的课程列表
     */
    @GetMapping("getMyCourseList")
    public HttpResult getMyCourseList(HttpServletRequest request)
    {
        HttpUser user = getUser(request);
        String username = user.getUsername();
        List<Course> list = courseService.selectByusername(username);
        return HttpResult.success("",list);
    }

    //获取学年
    @GetMapping("getSchoolYear")
    public HttpResult getSchoolYear(HttpServletRequest request)
    {
        List<Dicinfo> list = dicinfoService.getDicInfoList("major");
        return HttpResult.success("",list);
    }

    @PostMapping("deleteCourse")
    public HttpResult deleteCourse(@RequestBody ComFrontBean ComFrontBean,HttpServletRequest request)
    {
        int flag = courseService.deleteByPrimaryKey(ComFrontBean.getId());
        return flag==1 ? HttpResult.success("删除成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }
    //更新课程的live状态
    @PostMapping("updateCourseLive")
    public HttpResult updateCourseLive(@RequestBody ComFrontBean ComFrontBean,HttpServletRequest request)
    {
        Course c = courseService.selectByPrimaryKey(ComFrontBean.getId());
        c.setIslive(ComFrontBean.getType());
        int flag = courseService.updateCourse(c);
        return flag==1 ? HttpResult.success("更新成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }

    @PostMapping("saveCourse")
    public HttpResult saveCourse(@RequestBody Course course,HttpServletRequest request)
    {
    	HttpUser user = getUser(request);
        String username = user.getUsername();
        course.setUsername(username);
        int flag = courseService.insert(course);
        return flag==1 ? HttpResult.success("操作成功"):HttpResult.error(Constants.ERROR_MSG_25);
    }
}
